﻿using System.Collections.Generic;
using DimOS.Constants;

namespace DimOS.Internals
{
    public class Command
    {
        public string Key { get; set; }                             //Command's unique identifier

        public string[] Aliases { get; set; }                       //Set of alias names for Key
        
        public string Parent { get; set; }                          //Hashed(.) parenting command

        public delegate void Entry(List<string> parameters, string  //Delegate that describes the
        parent, bool silent, out string result, out Error error);   //entries in CommandEvaluator

        public Entry Action { get; set; }                           //Unique instance of delegate

        public string Description { get; set; }                     //Command's short description

        public Help Help { get; set; }                              //Comman's help(manual) entry

        public Command(string key, string[] aliases, Entry action, string description, Help help)
        {
            Key = key;
            Aliases = aliases;
            Action = action;
            Description = description;
            Help = help;
        }
 
        public static Command Find(int level, string key, out Error error)
        {
            error = Error.None;
            if (level >= Commands.List.Count)
            {
                error = Error.WrongLevel;
                return null;
            }
            for (int i = 0; i < Commands.List[level].Count; i++)
            {
                if (Commands.List[level][i].Key == key) return Commands.List[level][i];
                var aliases = Commands.List[level][i].Aliases;
                for (int j = 0; j < aliases.Length; j++) if (aliases[j] == key) return Commands.List[level][i];
            }
            error = Error.NotFound;
            return null;
        }
    }
}
